import { Component, Vue } from 'vue-property-decorator';

@Component({
    name: 'InstClass',
})
/**
 * 指令主页面公共类
 */
export default class InstClass extends Vue {
    // 显示的标签页
    activePane: string = '1';
    // 指令详情
    detail: any = {};
    // 是否已归档
    history: boolean = false;
    // 所有tabs标签页
    panes: any[] = [];
    staticResult: any = {};
    renderCount: number = 0;

    /**
     * 点击指令修改，跳转tab事件
     * @param val 要显示的表单数据
     */
    changeTab(val: any) {
        this.activePane = '1';
        this.detail = val;
    }

    /**
     * 重置指令表单
     * @param val 表单数据
     */
    reset(val: any) {
        this.detail = val;
    }

    /**
     * tab点击事件
     */
    handleTabClick() {
        if (this.activePane !== '1') {
            // 刷新指令管理表格数据
            this.renderCount++;
        }
    }

    /**
     * 指令结果查看详情按钮点击事件
     * @param
     * @returns
     */
    handelOperate({ record, history, clickRecord }): void {
        this.history = history;
        const parentRecord = clickRecord;
        const key = String(record.resultId + '-' + record.entryCode);

        for (let i = 0; i < this.panes.length; i++) {
            if (this.panes[i].key === key) {
                this.activePane = key;
                return;
            }
        }
        this.panes.push({ title: key, key: key });
        this.activePane = key;
        this.staticResult = record;
    }

    /**
     * 移除tabs
     * @param targetKey
     */
    removeTab(targetKey: string): void {
        let lastIndex;
        this.panes.forEach((pane, i) => {
            if (pane.key === targetKey) {
                lastIndex = i - 1;
            }
        });
        this.panes = this.panes.filter(pane => pane.key !== targetKey);
        if (this.panes.length && this.activePane === targetKey) {
            if (lastIndex >= 0) {
                this.activePane = this.panes[lastIndex].key;
            } else {
                this.activePane = this.panes[0].key;
            }
        }
        if (this.panes.length === 0) {
            this.activePane = '2';
        }
    }
}
